package com.smartwebx.ha.web.gis.module.screen;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.rainwater.feature.FeaturePumpManager;
import com.smartwebx.ha.biz.rainwater.real.PumpManager;
import com.smartwebx.ha.biz.rainwater.real.RiverFloodControlManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.annot.JsonModules;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.RiverFC;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.json.module.DoubleModule;

public class PumpScreen extends BasicScreen {
	@Autowired
	private PumpManager pumpManager;
	@Autowired
	private FeaturePumpManager featurePumpManager;
	@Autowired
	private RiverFloodControlManager riverFloodControlManager;

	/**
	 * 泵站日水位过程
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @param dxn
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {  
		List<DateValue> dateValueList = pumpManager
				.getSingleStatWaterDayProcess(stcd, bTm, eTm);// 泵站水位过程
		RiverFC riverFC = riverFloodControlManager
				.getSingleRiverFloodControl(stcd);// 河道防洪指标
		List<DateValue> dateValueFlowDays = pumpManager
				.getSingleStatFlowDayProcess(stcd, bTm, eTm);// 日流量过程
		List<DateValue> dateValueYears = featurePumpManager.getPeriodContrast(
				stcd, bTm, eTm);// 多年同期

		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		// 典型年
		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = pumpManager
				.getSingleStatWaterDayProcess(stcd, startDate, endDate);
		List<ViewProcessBean> viewProcess = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueWater : dateValueList) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);
			viewProcessBean.setTotalVal(riverFC == null ? 0 : riverFC
					.getWarWater());
			// 流量过程
			for (DateValue dateValueFlowDay : dateValueFlowDays) {
				if (Objects.equal(dateValueFlowDay.getTm(),
						dateValueWater.getTm())) {
					viewProcessBean.setValue5(dateValueFlowDay.getVal());
					break;
				}
			}
			// 多年同期
			for (DateValue dateValueYear : dateValueYears) {
				if (dateValueYear.getTm().compareTo(dateValueYear.getTm()) == 0) {
					viewProcessBean.setValue1(dateValueYear.getVal());
					break;
				}
			}
			// 典型年
			for (DateValue dxnDateValue : dxnDateValues) {
				String s1 = new SimpleDateFormat("MM-dd").format(dxnDateValue
						.getTm());
				String s2 = new SimpleDateFormat("MM-dd").format(dateValueWater
						.getTm());
				if (s1.equals(s2)) {
					viewProcessBean.setValue6(dxnDateValue.getVal());
					break;
				}
			}
			viewProcess.add(viewProcessBean);
		}

		return viewProcess;

	}
	
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		List<DateValue> dateValueList = pumpManager
				.getSingleStatWaterDayProcess(stcd, bTm, eTm);// 泵站水位过程
		RiverFC riverFC = riverFloodControlManager
				.getSingleRiverFloodControl(stcd);// 河道防洪指标
		List<DateValue> dateValueFlowDays = pumpManager
				.getSingleStatFlowDayProcess(stcd, bTm, eTm);// 日流量过程
		List<DateValue> dateValueYears = featurePumpManager.getPeriodContrast(
				stcd, bTm, eTm);// 多年同期

		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		// 典型年
		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = pumpManager
				.getSingleStatWaterDayProcess(stcd, startDate, endDate);
		List<Double> vals=Lists.newArrayList();//水位
		List<Double> totalVals=Lists.newArrayList();//警戒水位
		List<Double> values5=Lists.newArrayList();//流量过程
		List<Double> values1=Lists.newArrayList();//多年同期
		List<Double> values6=Lists.newArrayList();//典型年
		for (DateValue dateValueWater : dateValueList) {
			vals.add(dateValueWater.getVal());
			totalVals.add(riverFC == null ? 0 : riverFC
					.getWarWater());
			// 流量过程
			for (DateValue dateValueFlowDay : dateValueFlowDays) {
				if (Objects.equal(dateValueFlowDay.getTm(),
						dateValueWater.getTm())) {
					values5.add(dateValueFlowDay.getVal());
					break;
				}
			}
			// 多年同期
			for (DateValue dateValueYear : dateValueYears) {
				if (dateValueYear.getTm().compareTo(dateValueYear.getTm()) == 0) {
					values1.add(dateValueYear.getVal());
					break;
				}
			}
			// 典型年
			for (DateValue dxnDateValue : dxnDateValues) {
				String s1 = new SimpleDateFormat("MM-dd").format(dxnDateValue
						.getTm());
				String s2 = new SimpleDateFormat("MM-dd").format(dateValueWater
						.getTm());
				if (s1.equals(s2)) {
					values6.add(dxnDateValue.getVal());
					break;
				}
			}
		}

		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("vals", vals);
		result.put("totalVals", totalVals);
		result.put("values1", values1);
		result.put("values5", values5);
		result.put("values6", values6);
		return result;

	}

	/**
	 * 泵站小时过程
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<ViewProcessBean> getSingleStatHourProcess(String stcd,
			Date bTm, Date eTm) throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dateValueWaters = pumpManager.getPurenessWaterProcesss(
				stcd, bTm, eTm);// 泵站时水位过程
		List<DateValue> dateValueFlows = pumpManager.getPurenessFlowProcesss(
				stcd, bTm, eTm);// 泵站时流量过程
		List<ViewProcessBean> viewProcessList = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueWater : dateValueWaters) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);/// 当前水位
			// 泵站流量小时过程
			for (DateValue dateValueFlow : dateValueFlows) {
				if (dateValueWater.getTm().compareTo(dateValueFlow.getTm()) == 0) {
					viewProcessBean.setValue2(dateValueFlow.getVal());
					break;
				}
			}
			viewProcessList.add(viewProcessBean);
		}
		return viewProcessList;
	}
	
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public Object getSingleStatHourProcessByGisJs(String stcd,
			Date bTm, Date eTm) throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dateValueWaters = pumpManager.getPurenessWaterProcesss(
				stcd, bTm, eTm);// 泵站时水位过程
		List<DateValue> dateValueFlows = pumpManager.getPurenessFlowProcesss(
				stcd, bTm, eTm);// 泵站时流量过程
		List<Double> vals=Lists.newArrayList();//水位
		List<Double> values2=Lists.newArrayList();//流量
		for (DateValue dateValueWater : dateValueWaters) {
			vals.add(dateValueWater.getVal());/// 当前水位
			// 泵站流量小时过程
			for (DateValue dateValueFlow : dateValueFlows) {
				if (dateValueWater.getTm().compareTo(dateValueFlow.getTm()) == 0) {
					values2.add(dateValueFlow.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("vals", vals);
		result.put("values2", values2);
		return result;
	}


	/**
	 * 泵站日过程特征分析，历史最高、历史最低以及在历史中的排位
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd, Date bTm,
			Date eTm) {
		ViewDayProcessAnalysis viewDayProcessAnalysis = pumpManager
				.getDayProcessAnalysis(stcd, bTm, eTm);
		return viewDayProcessAnalysis;
	}
}
